package com.withyou.euraka.provide.web;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.withyou.euraka.provide.api.common.ResultVo;
import com.withyou.euraka.provide.api.common.Constant;
import com.withyou.euraka.provide.entity.UserEntity;
import com.withyou.euraka.provide.factory.ResultVoFactory;
import com.withyou.euraka.provide.service.UserInfoService;

@RestController
@RequestMapping("/api/v2")
public class UserInfoController {

	@Autowired
	private UserInfoService userInfoService;
	
	@ApiOperation(value="查询系统所有用户",notes="查询系统所有用户")
	@ApiResponses({
		@ApiResponse(code=500,message="服务异常")
	})
	@RequestMapping(value="/user",method=RequestMethod.GET)
	public ResultVo<List<UserEntity>> getAllUserList(){
		try {
			List<UserEntity> data = userInfoService.getAllUser();
			return ResultVoFactory.getUserEntitysSuccess(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultVoFactory.getUserEntitysError(500, Constant.SERVER_EXCEPTION);
		}
	}
	
	@ApiOperation(value="查询系统所有用户",notes="查询系统所有用户")
	@ApiResponses({
		@ApiResponse(code=500,message="服务异常")
	})
	@RequestMapping(value="/user/{id}",method=RequestMethod.GET)
	public ResultVo<List<UserEntity>> getUserInfo(@PathVariable(required=true) String id){
		try {
			List<UserEntity> data = userInfoService.getAllUser();
			return ResultVoFactory.getUserEntitysSuccess(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultVoFactory.getUserEntitysError(500, Constant.SERVER_EXCEPTION);
		}
	}
	
	@ApiOperation(value="注册用户",notes="注册用户")
	@ApiResponses({
		@ApiResponse(code=400,message="参数异常"),
		@ApiResponse(code=500,message="服务异常")
	})
	@RequestMapping(value="/user",method=RequestMethod.POST)
	public ResultVo<Boolean> registerUser(UserEntity userInfo){
		try {
			userInfoService.insertUser(userInfo);
			return ResultVoFactory.getSuccess();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultVoFactory.getError();
		}
	}
}
